package org.smr.ministore.micro;

import java.util.List;

import org.smr.ministore.dao.AuthorityMapper;
import org.smr.ministore.entities.Authority;
import org.smr.ministore.service.AuthorityService;

import org.blade.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@ConditionalOnProperty(name = "app.isDistributed", havingValue = "true")
public class AuthorityMicroService implements AuthorityService {

	@Autowired
	private AuthorityMapper mapper;

	@ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/Authority/batchSave")
	public int batchSave(@RequestBody List<Authority> entities) {
    	return mapper.batchInsert(entities);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT, value = "/Authority/save")
    public int save(@RequestBody Authority entity) {
    	return mapper.insert(entity);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.PATCH, value = "/Authority/updateById")
    public int updateById(@RequestBody Authority id) {
   	 	return mapper.updateById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/Authority/queryById/{id}")
    public Authority queryById( @PathVariable("id")Long id ) {
    	return (Authority) mapper.selectById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/Authority/deleteById/{id}")
    public int deleteById( @PathVariable("id")Long id ) {
        return mapper.deleteById(id);
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Authority/paginate/{pageSize}/{pageNum}")
    public Pager<Authority> paginate( Authority entity, @PathVariable("pageSize")Long pageSize,  @PathVariable("pageNum")Long pageNum ) {
        Pager<Authority> pager = new Pager(pageSize, pageNum);
        List<Authority> list = this.mapper.pagination( entity, pager );
        pager.setEntities(list);
        return pager;
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Authority/getByParentAndType/{parentId}/{type}")
    public List<Authority> getByParentAndType( @PathVariable("parentId")Long parentId,  @PathVariable("type")Integer type) {
        return this.mapper.selectByParentAndType(parentId, type);
    }
}
